package com.ruoyi.report.forms.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.report.forms.mapper.DispatchAccessMapper;
import com.ruoyi.report.forms.domain.DispatchAccess;
import com.ruoyi.report.forms.service.IDispatchAccessService;

/**
 * ????????״?Service业务层处理
 *
 * @author ruoyi
 * @date 2024-03-05
 */
@Service
public class DispatchAccessServiceImpl implements IDispatchAccessService {
    @Autowired
    private DispatchAccessMapper dispatchAccessMapper;

    /**
     * 查询????????״?
     *
     * @param id ????????״?主键
     * @return ????????״?
     */
    @Override
    public DispatchAccess selectDispatchAccessById(String id) {
        return dispatchAccessMapper.selectDispatchAccessById(id);
    }

    /**
     * 查询????????״?列表
     *
     * @param dispatchAccess ????????״?
     * @return ????????״?
     */
    @Override
    public List<DispatchAccess> selectDispatchAccessList(DispatchAccess dispatchAccess) {
        return dispatchAccessMapper.selectDispatchAccessList(dispatchAccess);
    }

    /**
     * 新增????????״?
     *
     * @param dispatchAccess ????????״?
     * @return 结果
     */
    @Override
    public int insertDispatchAccess(DispatchAccess dispatchAccess) {
        dispatchAccess.setCreateTime(DateUtils.getNowDate());
        return dispatchAccessMapper.insertDispatchAccess(dispatchAccess);
    }

    /**
     * 修改????????״?
     *
     * @param dispatchAccess ????????״?
     * @return 结果
     */
    @Override
    public int updateDispatchAccess(DispatchAccess dispatchAccess) {
        dispatchAccess.setUpdateTime(DateUtils.getNowDate());
        return dispatchAccessMapper.updateDispatchAccess(dispatchAccess);
    }

    /**
     * 批量删除????????״?
     *
     * @param ids 需要删除的????????״?主键
     * @return 结果
     */
    @Override
    public int deleteDispatchAccessByIds(String[] ids) {
        return dispatchAccessMapper.deleteDispatchAccessByIds(ids);
    }

    /**
     * 删除????????״?信息
     *
     * @param id ????????״?主键
     * @return 结果
     */
    @Override
    public int deleteDispatchAccessById(String id) {
        return dispatchAccessMapper.deleteDispatchAccessById(id);
    }

    @Override
    public List<DispatchAccess> listTree(DispatchAccess dispatchAccess) {
        List<DispatchAccess> list = dispatchAccessMapper.listTree(dispatchAccess);
        List<DispatchAccess> treeNodes = new ArrayList<DispatchAccess>();
        // 获取所有的根节点
        for (DispatchAccess treeRootNode : list) {
            if (treeRootNode.getParentId() == null) {
                continue;
            }
            if (treeRootNode.getParentId().equals(dispatchAccess.getParentId() != null ? dispatchAccess.getParentId() : "0")) {
                // 将所有子节点数据放进去
                treeRootNode = buildChildTree(treeRootNode, list);
                // 完成一个顶级节点所构建的树形，增加进来
                treeNodes.add(treeRootNode);
            }

        }
        return treeNodes;
    }

    public DispatchAccess buildChildTree(DispatchAccess pNode, List<DispatchAccess> list) {

        List<DispatchAccess> childTree = new ArrayList<DispatchAccess>();
        // list：所有节点集合（所有数据）
        for (DispatchAccess treeNode : list) {
            // 判断当前节点的父节点ID是否等于根节点的ID，即当前节点为其下的子节点
            if (treeNode.getParentId().equals(pNode.getId())) {
                // 再递归进行判断当前节点的情况，调用自身方法
                childTree.add(buildChildTree(treeNode, list));
            }
        }
        // for循环结束，即节点下没有任何节点，树形构建结束，设置树结果
        pNode.setDispatchAccessList(childTree);
        return pNode;
    }
}
